package com.iot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iot.core.LResult;
import com.iot.core.PageData;
import com.iot.mqtt.Server;
import com.iot.service.Tdecive_handleService;
import com.iot.service.Tnb_dev_jsonService;
import com.iot.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/nb/")
public class NbController extends BaseController {
    @Autowired
    private Tdecive_handleService service;
    @Autowired
    private Tnb_dev_jsonService nbJsonservice;

    @ResponseBody
    @RequestMapping("open")  //type b蓝牙 r 远程 c 二维码
    public String open(Integer userId, Integer empId, String imei, Integer door, String type) {
        try {

            service.open(userId, empId, imei, door, type);
            return LResult.Success("请按锁，打开柜门！").toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("getUpData")  //type b蓝牙 r 远程 c 二维码
    public String getUpData() {
        try {

            service.getUpData();
            return LResult.Success("请按锁，打开柜门！").toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("n00")
    public String n00(String imei) {
        String dt = Common.getLongDate(new Date());
        try {
            Server server = new Server();
            String message = "{\"imei\":\"" + imei + "\",\"messageId\":\"00\",\"messageTime\":\"" + dt + "\"}";
            server.publish("NB_DEV_UP_DATA", message, 1);
            return LResult.Success("MQ服务器已经下达00命令").toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("n81")
    public String n81(String imei) {
        try {
            String dt = Common.getLongDate(new Date());
            Server server = new Server();
            String message = "{\"imei\":\"" + imei + "\",\"messageId\":\"81\",\"errCode\":0,\"rspData\":\"OK\",\"messageTime\":\"" + dt + "\"}";
            server.publish("NB_DEV_UP_DATA", message, 1);

            return LResult.Success("MQ服务器已经下达81命令").toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("mean01")
    public String mean01(String imei) {
        try {
            String dt = Common.getLongDate(new Date());
            Server server = new Server();
            String message = "{\"imei\":\"" + imei + "\",\"messageId\":\"02\",\"meanCode\":1,\"doorLock\":128,\"soaking\":0,\"batteryLevel\":90,\"temperature\":18,\"humidity\":50,\"quake\":20,\"tilt\":0,\"lockStatus\":0,\"messageTime\":\"" + dt + "\"}";
            server.publish("NB_DEV_UP_DATA", message, 1);

            return LResult.Success("MQ服务器已经下达81命令").toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("mean00")
    public String mean00(String imei) {
        try {
            String dt = Common.getLongDate(new Date());
            Server server = new Server();
            String message = "{\"imei\":\"" + imei + "\",\"messageId\":\"02\",\"meanCode\":0,\"doorLock\":128,\"soaking\":0,\"batteryLevel\":90,\"temperature\":18,\"humidity\":50,\"quake\":20,\"tilt\":0,\"lockStatus\":0,\"messageTime\":\"" + dt + "\"}";
            server.publish("NB_DEV_UP_DATA", message, 1);

            return LResult.Success("心跳数据").toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }
    @ResponseBody
    @RequestMapping("jsonData")
    public String jsonData() {

        try {
            PageData map = getPageData();
            if (!map.containsKey("appId")) {
                map.put("appId", 0);
            }

            if (!map.containsKey("sdate")) {
                map.put("sdate", "");
            }
            if (!map.containsKey("edate")) {
                map.put("edate", "");
            } else {
                String strSdate = map.get("edate").toString();
                if (strSdate != "") {
                    String tempdate = Common.getShortDate(Common.getSomeDay(strSdate, 1));
                    map.put("edate", tempdate);
                }
            }
            if (!map.containsKey("typeId")) {
                map.put("typeId", 0);
            }

            PageHelper.startPage(Common.toInt(map.get("page")), Common.toInt(map.get("limit")));
            List<Map<String, Object>> list = nbJsonservice.fetchByPage(map);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
            return LResult.Success(pageInfo.getTotal(), pageInfo.getList()).toJson();


        } catch (Exception ex) {
            return LResult.Error( "数据错误").toJson();
        }
    }

}
